package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.dto.R;
import com.qf.pojo.Book;
import com.qf.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/books")
public class BookController {

    private BookService bookService;

    @Autowired
    public void setBookService(BookService bookService) {
        this.bookService = bookService;
    }

    @PostMapping("/newbook")
    public R addBook(@RequestBody Book book){
        bookService.addBook(book);
        return R.setOk(null);
    }

    @GetMapping("/books/{name}/{pageNum}/{pageSize}")
    public R getAllBooksByPage(@PathVariable String name,@PathVariable @RequestParam(defaultValue = "1")int pageNum,@PathVariable @RequestParam(defaultValue = "10")int pageSize){
        System.err.println("=========================="+"name = " + name + ", pageNum = " + pageNum + ", pageSize = " + pageSize);
        PageInfo<Book> result = bookService.findAllBookByPage(name,pageNum,pageSize);
        return R.setOk(result);
    }
}
